/**
 * @param {number[][]} matrix
 * @return {number[]}
 */
var getMaxMatrix = function (matrix) {
  const dp = new Array(matrix.length);
  let result = matrix[0][0],
    direction = [0, 0, 0, 0];
  // 定义二维数组
  for (let i = 0; i < matrix[0].length; i++) {
    dp[i] = new Array(matrix[0].length);
  }
  // 计算和
  function sum(indexI, indexJ) {
    let num = 0;
    for (let i = 0; i <= indexI; i++) {
      for (let j = 0; j <= indexJ; j++) {
        num += matrix[i][j];
      }
    }
    return num;
  }
  // 初始化
  for (let i = 0; i < matrix.length; i++) {
    for (let j = 0; j < matrix[0].length; j++) {
      if (i == 0 && j == 0) {
        dp[i][j] = matrix[0][0];
      } else if (j > 0 && i == 0) {
        dp[i][j] = Math.max(dp[i][j - 1] + matrix[i][j], matrix[i][j]);
      } else if (j == 0 && i > 0) {
        dp[i][j] = Math.max(dp[i - 1][j] + matrix[i][j], matrix[i][j]);
      } else {
        dp[i][j] = sum(i, j);
      }
      if (dp[i][j] > result) {
        direction = [];
      }
      result = Math.max(result, dp[i][j]);
    }
  }
  return result;
};

let matrix = [
  [-1, 0],
  [0, -1],
];
console.log(getMaxMatrix(matrix));
matrix = [
  [-1, 2],
  [0, 3],
];
console.log(getMaxMatrix(matrix));
